package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyzy.domain.dto.ResponseData;
import com.cyzy.domain.query.TLandQuery;
import com.cyzy.domain.vo.*;
import com.cyzy.mapper.*;
import com.cyzy.service.MyLandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyLandImpl implements MyLandService {
//    土地
    @Autowired
    private MyLandMapper myLandMapper;
    //    地区
    @Autowired
    private RegionMapper regionMapper;
    //    种植
    @Autowired
    private CultivationMapper cultivationMapper;
    //    用户
    @Autowired
    private UserMapper userMapper;
    //    基地
    @Autowired
    private LandMapper landMapper;
    //    日志操作记录
    @Autowired
    private LogMapper logMapper;
    //    插入仓库数据
    @Autowired
    private WarehouseMapper warehouseMapper;

//    查询用户是否有租赁土地
    @Override
    public ResponseData select(TLandQuery tLandQuery) {
        ResponseData responseData =new ResponseData();
//        没有用户结束
        if (tLandQuery.getUserId() == 0){
            responseData.setCode(400);
            return responseData;
        }
//        查询用户数据
//        TUserVo tUser = userMapper.selectById(tLandQuery.getUserId());
        //分页器
        Page<TLand> page = new Page<>(tLandQuery.getCurrentPage(),tLandQuery.getSize());
        //mybatis-plus  自定义where条件
        QueryWrapper<TLand> wrapper = new QueryWrapper<>();
        if (tLandQuery.getType() != 0) {
            wrapper.eq("land_type", tLandQuery.getType());
        }
        if (tLandQuery.getName() != null) {
            wrapper.like("land_name", tLandQuery.getName());
        }
        // 查询分页数据
        Page<TLand> plantPage = myLandMapper.selectPage(page, wrapper);
        // 获取分页数据列表
        List<TLand> data = plantPage.getRecords();
        responseData.setCode(200);
        responseData.setData(data);
        return responseData;
    }
//查询详细数据
    @Override
    public ResponseData mylandid(TLandQuery tLandQuery) {
        System.out.println(tLandQuery.getLandId());
//        查询
        TLand tLand = myLandMapper.selectById(tLandQuery.getLandId());
//        种植数据
        TCultivation tCultivation =cultivationMapper.selectById(tLand.getCultivationId());
        tLand.setTCultivation(tCultivation);
//        用户数据
        TUserVo tUser = userMapper.selectById(tLand.getUserId());
        tLand.setTUserVo(tUser);
//        区域数据
        TRegion tRegion = regionMapper.selectById(tLand.getRegionId());
        tLand.setTRegion(tRegion);
//        地基数据
        TBase tBase = landMapper.selectById(tLand.getBaseId());
        tLand.setTBase(tBase);
//        存入集合
        List<TLand> data= new ArrayList<>();
        data.add(tLand);
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setData(tLand);
        return responseData;
    }
//服务状态修改状态
    @Override
    public ResponseData myservice(TLand tLand) {
        String service = tLand.getServiceType();
        TLand tLand1 = myLandMapper.selectById(tLand.getLandId());
        tLand1.setServiceType(service);
        myLandMapper.updateById(tLand1);

        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        return responseData;
    }
//浇水
    @Override
    public ResponseData mywatering(TLand tLand) {
            TLand tLand1 = myLandMapper.selectById(tLand.getLandId());
            tLand1.setWater(tLand.getWater());
            myLandMapper.updateById(tLand1);
//            插入日志操作
            TLog tLog = new TLog();
            tLog.setOperation("浇水");
            tLog.setLandId(tLand.getLandId());
            tLog.setUserId(tLand.getUserId());
            logMapper.insert(tLog);
// 更新数据
        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        return responseData;
    }
//种植和收获
    @Override
    public ResponseData myplant(TLand tLand) {
        TLand tLand1 = myLandMapper.selectById(tLand.getLandId());

        ResponseData responseData = new ResponseData();
        TLog tLog = new TLog();
//        如果是种植进行插入日志操作
        if (tLand.getWeatherInfo().equals("种植")){
            //        进行插入
            TCultivation tCultivation = new TCultivation();
            tCultivation.setPet("无害");
            tCultivation.setGrowp("正常生长");
            tCultivation.setPlantstatus("苗期");
            tCultivation.setPlantName(tLand.getServiceType());
            cultivationMapper.insert(tCultivation);
//            获取到生成的种植id
            Long generatedId = tCultivation.getCultivationId();
            tLand1.setCultivationId(generatedId);
            myLandMapper.updateById(tLand1);

            tLog.setOperation(tLand.getWeatherInfo());
            tLog.setLandId(tLand.getLandId());
            tLog.setUserId(tLand.getUserId());
            logMapper.insert(tLog);

            responseData.setCode(200);
        }
//        执行放入到仓库操作   明早补充
        if (tLand.getWeatherInfo().equals("收获")){
            tLand1.setCultivationId(tLand.getCultivationId());
            myLandMapper.updateById(tLand1);

            tLog.setOperation(tLand.getWeatherInfo());
            tLog.setLandId(tLand.getLandId());
            tLog.setUserId(tLand.getUserId());
            logMapper.insert(tLog);

// 1. 根据 regionId、warehouseName 和 userId 查询是否已有相同的仓库记录
            QueryWrapper<TWarehouseVo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("warehouse_description", tLand.getServiceType())          // 区域 ID
                    .eq("warehouse_name", tLand.getServiceType())  // 植物 名称
                    .eq("user_id", tLand.getUserId());             // 用户 ID
// 查找匹配的仓库记录
            TWarehouseVo existingWarehouse = warehouseMapper.selectOne(queryWrapper);

// 2. 判断是否找到已有记录
            if (existingWarehouse != null) {
                // 如果找到已有记录，更新该记录的 Plantnum，增加 50
                existingWarehouse.setPlantnum(existingWarehouse.getPlantnum() + 50);
                warehouseMapper.updateById(existingWarehouse);
            } else {
                // 如果没有找到匹配记录，则插入一条新的记录
                TWarehouseVo tWarehouseVo = new TWarehouseVo();
                tWarehouseVo.setUserId(tLand.getUserId());
                tWarehouseVo.setWarehouseName(tLand.getServiceType());
                tWarehouseVo.setRegionId(tLand.getRegionId());
                tWarehouseVo.setWarehouseDescription(tLand.getWeather());
                tWarehouseVo.setPlantnum(50);  // 初始值设置为 50

                warehouseMapper.insert(tWarehouseVo);
            }
            responseData.setCode(200);
        }
        return responseData;
    }
}
